using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace Smokey.Client.Linq {
   public partial class ModelDataContext {
        private DBSession dbSession;

        protected internal ModelDataContext(DBSession dbSession) :
            base(GetConnectionFromSession(dbSession), mappingSource) {
            this.dbSession = dbSession;
        }

        public static IDbConnection GetConnectionFromSession(DBSession session) {
            return session.Connection;
        }

        public new void SubmitChanges() {
            base.SubmitChanges(ConflictMode.FailOnFirstConflict);
        }

        public void SubmitChangesAndCompleteTransaction() {
            this.SubmitChanges();
            this.dbSession.CompleteTransaction();
        }

        public DBSession DBSession {
            get {
                return dbSession;
            }
        }

        public static ModelDataContext CreateDataContext(DBSession session) {
            try {
                return new ModelDataContext(session);
            } catch {
                session.Dispose();
                throw;
            }
        }
    }
}
